<template>
  <div class="theme-switcher">
    <label class="switch">
      <input type="checkbox" v-model="isDarkMode" @change="onThemeChange">
      <span class="slider round"></span>
    </label>
    <span>{{ isDarkMode ? '深色模式' : '浅色模式' }}</span>
  </div>
</template>

<script setup>
import { ref, watch } from 'vue'

// Props定义
const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false
  }
})

// Emits定义
const emit = defineEmits(['update:modelValue', 'theme-change'])

// 响应式状态
const isDarkMode = ref(props.modelValue)

// 监听props变化，同步到本地状态
watch(() => props.modelValue, (newValue) => {
  isDarkMode.value = newValue
})

// 处理主题变化
const onThemeChange = () => {
  // 发出更新事件
  emit('update:modelValue', isDarkMode.value)
  // 发出主题变化事件
  emit('theme-change', isDarkMode.value)
}
</script>

<style scoped>
.theme-switcher {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px;
  border-radius: 8px;
  background-color: #f5f5f5;
}

.switch {
  position: relative;
  display: inline-block;
  width: 50px;
  height: 24px;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  transition: .4s;
  border-radius: 24px;
}

.slider:before {
  position: absolute;
  content: "";
  height: 18px;
  width: 18px;
  left: 3px;
  bottom: 3px;
  background-color: white;
  transition: .4s;
  border-radius: 50%;
}

input:checked + .slider {
  background-color: #2196F3;
}

input:checked + .slider:before {
  transform: translateX(26px);
}

/* 深色模式下的样式 */
:global(.dark-mode) .theme-switcher {
  background-color: #333;
  color: #fff;
}
</style>